<?php

class Admin_Model_Groups extends Zend_Db_Table_Abstract {

    /**
     * Model of user's groups
     *
     * @author Nguyen Manh Hung
     */
    protected $_name = TABLE_USERS_GROUPS;
    protected $_id = 'id';

    /**
     *
     * @author Nguyen manh Hung
     */
    public function insertGroups($name, $description = "", $status = 1, $admin = 0) {
        $dateCreated = new Zend_Db_Expr('NOW()');

        $db = Zend_Db_Table::getDefaultAdapter();

        $row = $this->createRow();

        $row->name = $name;

        $row->description = $description;

        $row->active = $status;

        $row->admin = $admin;

        $row->date_created = $dateCreated;

        $row->save();

        $id = $row->id;

        return $id;
    }

    /**
     * update menu
     *
     * @author Nguyen Manh Hung
     */
    public function updateGroup($id, $name, $description = "", $status = 1, $admin = 0) {
        $dateCreated = new Zend_Db_Expr('NOW()');
        
        $db = Zend_Db_Table::getDefaultAdapter();

        $row = $this->find($id)->current();

        if ($row) {

            $row->name = $name;

            $row->description = $description;

            $row->active = $status;

            $row->admin = $admin;

            $row->date_created = $dateCreated;

            $row->save();

            return 1;
        } else {

            return 0;
        }
    }

    public function updatePermissions($id, $permissions = '') {
        $dateCreated = new Zend_Db_Expr('NOW()');
        
        $db = Zend_Db_Table::getDefaultAdapter();

        $row = $this->find($id)->current();

        if ($row) {
            
            $row->permissions = $permissions;
            
            $row->date_created = $dateCreated;

            $row->save();

            return 1;
        } else {

            return 0;
        }
    }

    /**
     * delete Menu
     *
     * @author Nguyen Manh HUng
     */
    public function deleteGroup($id) {
        $db = Zend_Db_Table::getDefaultAdapter();

        $id = $db->quote($id, INTEGER);

        $row = $this->find($id)->current();

        if ($row) {

            $row->delete();

            return 1;
        } else {

            return 0;
        }
    }

    /**
     * fetch all menu
     *
     * @author Nguyen Manh Hung (manhhung86it@gmail.com)
     */
    public function fetchGroup($filters = array(), $sortFeild = null, $limit = null, $page = 1) {
        $select = $this->select();

        // add cac filter vao truy van tim kiem

        if (count($filters) > 0) {

            foreach ($filters as $feild => $filter) {

                $select->where($feild . ' =?', $filter);
            }
        }

        // add sort vao truy van

        if (null != $sortFeild) {

            $select->order($sortFeild);
        }

        // add limit - offset vao truy van

        if (null != $limit) {

            $offset = $page * NUM_PER_PAGE;

            if ($offset <= $limit) {

                $offset = $limit + NUM_PER_PAGE;
            }

            $select->limit($limit, $offset);
        }

        return $this->fetchAll($select);
    }

}